# load packages
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(plotly)
##
## Attaching package: 'plotly'
##
## The following object is masked from 'package:ggplot2':
##
## last_plot
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following object is masked from 'package:graphics':
##
## layout
library(sf)
## Linking to GEOS 3.10.2, GDAL 3.4.1, PROJ 8.2.1; sf_use_s2() is TRUE
library(ggspatial)
library("viridis")
## Loading required package: viridisLite
# load data
data = read_rds("state_crime_unemp.rds")
# filter data to specified states
filtered_data <- filter(data, NAME == "Illinois" | NAME == "California" |
NAME == "Idaho" | NAME == "Indiana")
# plot unemployment over time
unemployment_plot <- plot_ly(filtered_data, x = ~Year, y = ~unemp_rate,
color = ~NAME) %>%
add_lines()
unemployment_plot<- unemployment_plot %>% layout(title = 'Unemployment Rate over Time',
xaxis = list(title = 'Year'),
yaxis = list (title = 'Unemployment Rate'))
unemployment_plot
# plot crime over time
crime_plot <- plot_ly(filtered_data, x = ~Year, y = ~crime_rate,
color = ~NAME) %>%
add_lines()
crime_plot<- crime_plot %>% layout(title = 'Crime Rate over Time',
xaxis = list(title = 'Year'),
yaxis = list (title = 'Crime Rate'))
crime_plot
# Filter Data to 2014
data_2014 = filter(data, Year == 2014)
#plot crime vs unemployment scatter plot
crime_vs_unemployment_plot <- plot_ly(filtered_data, x = ~crime_rate, y = ~unemp_rate, color = ~NAME) %>%
add_markers()
crime_vs_unemployment_plot <- crime_vs_unemployment_plot %>% layout(title = 'Crime Rate over Time',
xaxis = list(title = 'Year'),
yaxis = list (title = 'Crime Rate'))
crime_vs_unemployment_plot
# plot spatial unemployment map
unemp_map = ggplot() +
geom_sf(data=data_2014,aes(fill=(unemp_rate)))+
scale_fill_viridis(option = "D")+
annotation_scale(location = "bl")+ #0.002, upper,and 0.05 lower
annotation_north_arrow(location = "br", which_north = "true",
style = north_arrow_fancy_orienteering) +
guides(fill=guide_legend(title="Unemployment Rate in 2014"))+
xlab("Longitude") + ylab("Latitude") +
ggtitle("Unemployment Rate Map Over Contiguous US")
unemp_map
## Scale on map varies by more than 10%, scale bar may be inaccurate

# plot spatial crime map
crime_map = ggplot() +
geom_sf(data=data_2014,aes(fill=crime_rate))+
scale_fill_viridis()+
annotation_scale(location = "bl")+ #0.002, upper,and 0.05 lower
annotation_north_arrow(location = "br", which_north = "true",
style = north_arrow_fancy_orienteering) +
guides(fill=guide_legend(title="Crime Rate in 2014"))+
xlab("Longitude") + ylab("Latitude") +
ggtitle("Crime Rate Map Over Contiguous US")
crime_map
## Scale on map varies by more than 10%, scale bar may be inaccurate
